#define _CRT_SECURE_NO_WARNINGS 

/*
struct TreeLinkNode {
    int val;
    struct TreeLinkNode *left;
    struct TreeLinkNode *right;
    struct TreeLinkNode *next;
    TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {

    }
};
*/
class Solution {
public:
    TreeLinkNode* GetNext(TreeLinkNode* pNode) {
        if (pNode == NULL)
            return NULL;
        TreeLinkNode* pret = pNode;
        if (pNode->right) {
            pret = pNode->right;
            while (pret->left) {
                pret = pret->left;
            }
            return pret;
        }
        while (pNode->next) {
            pret = pNode->next;
            if (pret->left == pNode)
                return pret;
            pNode = pNode->next;
        }
        return NULL;
    }
};
